#ifndef __FD_MANGER_H__
#define __FD_MANGER_H__

#include <memory>
#include "thread.h"
#include "iomanger.h"

namespace sylar
{

class FdCtx : public std::enable_shared_from_this<FdCtx>
{
public:
    using ptr = std::shared_ptr<FdCtx>;

    FdCtx(int fd);
    ~FdCtx();

    bool init();

    bool isInit() const { return m_isInit; }
    bool isScoket() const {  return m_isSocket; }
    bool isClose() const  { return m_isClosed; }

    void setUserNoBlock(bool v) { m_isUserNoBlock = v; }
    bool getUserNoBlock() const {  return m_isUserNoBlock; }

    void setSysNoBlock(bool v) { m_isSysNoBlock = v; }
    bool getSysNoBlock() const { return m_isSysNoBlock; }

    void setTimeOut(int type, uint64_t v);
    void getTimeOut(int type);



private:
    bool m_isInit : 1;
    bool m_isSocket : 1;
    bool m_isSysNoBlock : 1;
    bool m_isUserNoBlock : 1;
    bool m_isClosed : 1;
    int m_fd = -1;
    uint64_t m_recvTimeout;
    uint64_t m_sendTimeout;

    sylar::IOManger* m_iomanger;
};

class FdManger
{
public:
    using RWMutexType = RWMutex;
    FdManger();
    FdCtx::ptr get(int fd, bool auto_create = false);

private:
    RWMutexType m_mutex;
    std::vector<FdCtx::ptr> m_datas;
};

}
#endif